<template>
  <div id="detail">
    <detail-nav-bar @titleClick="titleClick" ref="nav"></detail-nav-bar>
    <scroll class="content" ref="scroll" :probe-type="3" @scroll="contentScroll">
      <detail-swiper :top-images="topImages"></detail-swiper>
      <detail-base-info :goods="goods"></detail-base-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad"></detail-goods-info>
      <detail-param-info :param-info="paramInfo" ref="param"></detail-param-info>
      <detail-comment-info :comment-info="commentInfo" ref="comment"></detail-comment-info>
      <goods-list ref="recommend" :goods="recommends" class="a"/>
    </scroll>
    <detail-buttom-bar @addCart="addToCart"/>
    <back-top @click.native="backTop" v-show="isShowBackTop"/>
  </div>
</template>
<script>
import DetailNavBar from './childComps/DetailNavBar'
import DetailSwiper from './childComps/DetailSwiper'
import DetailBaseInfo from './childComps/DetailBaseInfo'
import DetailShopInfo from './childComps/DetailShopInfo'
import DetailGoodsInfo from './childComps/DetailGoodsInfo'
import DetailParamInfo from './childComps/DetailParamInfo'
import GoodsList from '../../components/content/goods/GoodsList'
import DetailButtomBar from './childComps/DetailButtomBar'
import Scroll from '../../components/common/scroll/Scroll'
import {backTopMiXin, itemListenerMixin} from '../../common/mixin'
import {getDetail, Goods, Shop, GoodsParam, getRecommend} from '../../network/detail'
import DetailCommentInfo from './childComps/DetailCommentInfo'
import {debounce} from '../../common/utils'
import {mapActions} from 'vuex'
export default {
  name: 'Detail',
  components: {
    DetailButtomBar,
    DetailNavBar,
    DetailSwiper,
    GoodsList,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    Scroll
  },
  mixins: [itemListenerMixin, backTopMiXin],
  data () {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [],
      getThemeTopY: null,
      currentIndex: 0
    }
  },
  destroyed () {
    this.$bus.$off('itemImgLoad', this.itemImgListener)
  },
  mounted () {
  },
  created () {
    //1.保存传入的iid
    this.iid = this.$route.params.iid
    //2.根据iid请求数据
    getDetail(this.iid).then(res => {
      //1.获取顶部图片的轮播数据
      const data = res.result
      this.topImages = data.itemInfo.topImages
      //2.获取商品信息
      this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)
      //3.创建店铺信息对象
      this.shop = new Shop(data.shopInfo)
      //4.保存商品的详情数据
      this.detailInfo = data.detailInfo
      //5.获取商品的参数信息
      this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)
      //6.获取商品评论
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0]
      }
    })
    //3.通过接口获取推荐数据
    getRecommend().then(res => {
      this.recommends = res.data.list
    })
    //4.赋值
    this.getThemeTopY = debounce(() => {
      this.themeTopYs = []
      this.themeTopYs.push(0)
      this.themeTopYs.push(this.$refs.param.$el.offsetTop)
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)
    }, 100)
  },
  methods: {
    ...mapActions(['addCart']),
    imageLoad () {
      this.newRefresh()
      this.getThemeTopY()
    },
    titleClick (index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 200)
    },
    contentScroll (position) {
      const positoinY = -position.y
      let length = this.themeTopYs.length
      for (let i = 0; i < length; i++) {
        if (this.currentIndex !== i && ((i < length - 1 && positoinY >= this.themeTopYs[i] && positoinY < this.themeTopYs[i + 1])
          || (i === length - 1 && positoinY >= this.themeTopYs[i]))) {
          this.currentIndex = i
          this.$refs.nav.currentIndex = this.currentIndex
        }

      }
      //3.判断backtop是否显示
      this.isShowBackTop = (-position.y) > 1000
      // 决定tabControl是否吸顶（position：fixed）
      this.isTabFixed = (-position.y) > (this.tabOffsetTop + 44)
    },
    addToCart () {
     //1.获取购物车需要展示信息
      const product = {}
      product.image = this.topImages[0]
      product.title = this.goods.title
      product.desc = this.goods.desc
      product.price = this.goods.realPrice
      product.iid = this.iid
      //2.将商品添加到购物车
     // this.$store.dispatch('addCart',product).then(
     //   res => {
     //     // 添加到购物车成功
     //    this.$toast.show(res,1500)
     //   }
     // )
      this.addCart(product).then(res => {
        // 添加到购物车成功
        this.$toast.show(res,1500)
      })

    }
  }
}
</script>

<style scoped>
#detail {
  position: relative;
  z-index: 9;
  background-color: #fff;
  height: 100vh;
}

.content {
  height: calc(100% - 43px);
  padding-bottom: 49px;
}

.a {
  padding-bottom: 50px;
}
</style>
